<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
  $cururl="";
  $mode=-1;
  $meth=1;
  $qry="";
  $ans="";
  $doc=null;
  $title="Web Browser";
  $xbody=null;
  $curhost="";
  $curpath="";
  
  function get($qry,$host)
  {
    $errno=0;
	$errstr="";
	$sock=fsockopen($host, 80, $errno, $errstr, 60);
	if (!$sock)
	{
	 $errstr=iconv("cp1251","utf-8",$errstr);
	 return sprintf("Error %d: %s\r\n\r\n",$errno,$errstr);
	}
	fwrite($sock,$qry);
	$ans="";
	while (!feof($sock)) {
        $ans.=fgets($sock, 256);
		printf(" ");
    }
	fclose($sock);
	//$pans=iconv("cp1251","utf-8",$ans);
	return $ans;
  }
  
  function normalizeUrl($url)
  {
    if (strpos($url,"http://")===false)
	  $url="http://".$url;
	return $url;
  }
  
  function makeGet($url)
  {
    $url=substr($url,7);
	$paths=explode('/',$url);
	$host=$paths[0];
	$path=substr($url,strlen($host));
    $qry="GET ".$path." HTTP/1.1\r\n";
	$qry.="Host: ".$host."\r\n";
	if (isset($_GET['referer']))
	  $qry.="Referrer: ".$_GET['referer']."\r\n";
	$qry.="\r\n";
	return array($qry,$host);
  }
  
  function makePost($url)
  {
    $url=substr($url,7);
	$paths=explode('/',$url);
	$host=$paths[0];
	$path=substr($url,strlen($host));
	$params=explode('?',$path);
	$path=$params[0];
	$params=$params[1];
    $qry="POST ".$path." HTTP/1.1\r\n";
	$qry.="Host: ".$host."\r\n";
	if (isset($_GET['referer']))
	  $qry.="Referrer: ".$_GET['referer'];
	$qry.="Content-Length: ".strlen($params)."\r\n";
	$qry.="\r\n".$parms."\r\n";
	$qry.="\r\n\r\n";
	return array($qry,$host);
  }
  
  if (isset($_GET['url']))
  {
    $cururl=normalizeUrl($_GET['url']);
	$mode=$_GET['viewAs'];
	if (isset($_GET['method']))
	  if ($_GET['method']=="post")
	    $meth=2;
	$host="";
	$qry=($meth==2)?makePost($cururl):makeGet($cururl);
	$curhost=$qry[1];
	$data=get($qry[0],$curhost);
	$f=fopen("D:/temp/data.log","w");
	fwrite($f,$data);
	fclose($f);
	$qry=$qry[0];
	$sdata=explode("<html",$data);
//	if (count($sdata)<2)
//	 $sdata=explode("\n\n",$data);
	$ans=$sdata[0];
	$body="<html".$sdata[1];
	if ($body)
	{
	 $body=mb_convert_encoding($body, 'utf-8', 'windows-1251');
	 $doc=new DOMDocument("1.0","utf-8");
	 $doc->encoding="utf-8";
	 @$doc->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$body);
//	 @$doc->loadHTML($body);
	if ($doc)
	{
		$head=null;
		$nd=$doc->documentElement->firstChild;
		while($nd)
		{
		  if (strtolower($nd->nodeName)=="head")
		    $head=$nd;
		  if (strtolower($nd->nodeName)=="body")
		    $xbody=$nd;
		  $nd=$nd->nextSibling;
		}
		if ($head)
		{
		  $ans.="<br>";
		  $nd=$head->firstChild;
		  while($nd)
		  {
			if (strtolower($nd->nodeName)=="title")
			  $title=$nd->firstChild->nodeValue;
			if (strtolower($nd->nodeName)=="meta")
			{
				if ($nd->hasAttribute('http-equiv'))
				    if ($nd->getAttribute('content')!="text/html; charset=utf-8")
					$ans.=sprintf("<br>he_%s=%s",$nd->getAttribute('http-equiv'),$nd->getAttribute('content'));
				if ($nd->hasAttribute('name'))
					$ans.=sprintf("<br>%s=%s",$nd->getAttribute('name'),$nd->getAttribute('content'));
			}
		    $nd=$nd->nextSibling;
		  }
		}
	}
	}
  }
?>
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF8">
<META http-equiv="Content-Language" content="ru">
<title>web browse</title>
<script type="text/javascript">
	function mkHelpStr(str,event)
	{
		document.helper.innerHTML=str;
		document.helper.style.position.top=event.Y+5;
		document.helper.style.position.left=event.X+5;
	}
	function hideHelpStr()
	{
		document.helper.style.visible=false;
		document.helper.style.display='none';
	}
</script>
</head>
<body>
<table width="100%" style="width:100%;" cellpadding="0" cellspacing="0">
<tr style="background-color:grey;"><td><?php echo $title;?></td></tr>
<tr><td>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="hidden" name="method" value="get">
<table width="100%" style="width:100%;"><tr><td>url:</td><td width="100%" style="width:100%;">
<input type="text" name="url" width="100%" style="width:100%;" value="<?php echo $cururl;?>">
</td><td>
<input type="submit" name="" value="go">
</td><td>
<select name="viewAs">
<option value="0" <?php if ($mode==0) echo "selected"?>>yandex</option>
<option value="1" <?php if ($mode==1) echo "selected"?>>google</option>
</select>
</td></tr></table>
</form>
</td></tr>
<tr style="background-color:grey;"><td><?php
 printf("%s%s",str_replace("\r\n","<br>",$qry),str_replace("\r\n","<br>",$ans));
?></td></tr>
<tr><td>
<?php
  function nuNode($node,$name,$copyAttr=null,$nuAttr=null,$helpstr="")
  {
	$doc=$node->ownerDocument;
	$par=$node->parentNode;
	$nd=$doc->createElement($name);
	if ($copyAttr)
	foreach ($copyAttr as $nm)
	if ($node->hasAttribute($nm))
	{
	  $atr=$doc->createAttribute($nm);
	  $atr->appendChild($doc->createTextNode($node->getAttribute($nm)));
	  $nd->appendChild($atr);
	}
	if ($nuAttr)
	foreach($nuAttr as $nm=>$vl)
	{
	  $atr=$doc->createAttribute($nm);
	  $atr->appendChild($doc->createTextNode($vl));
	  $nd->appendChild($atr);
	}
	if ($helpstr!="")
	{
	  $atr=$doc->createAttribute("title");
	  $atr->appendChild($doc->createTextNode($helpstr));
	  $nd->appendChild($atr);
		/*
	  $atr=$doc->createAttribute("onmouseover");
	  $atr->appendChild($doc->createTextNode(sprintf("mkHelpStr('%s',event); return false;",$helpstr)));
	  $nd->appendChild($atr);
	  $atr=$doc->createAttribute("onmouseout");
	  $atr->appendChild($doc->createTextNode("hideHelpStr(); return false;"));
	  $nd->appendChild($atr);
	  */
	}
	$cnd=$node->firstChild;
	while($cnd)
	{
		$nd->appendChild($cnd->cloneNode(true));
		$cnd=$cnd->nextSibling;
	}
	$par->replaceChild($nd,$node);
	//$par->removeChild($node);
	return $nd;
  }
  
  function killNode($node)
  {
	$par=$node->parentNode;
	$cnd=$node->firstChild;
	$next=null;
	while($cnd)
	{
		$pnd=$cnd->cloneNode(true);
		if (!$next)
		  $next=$pnd;
		$par->insertBefore($pnd,$node);
		$cnd=$cnd->nextSibling;
	}
	$par->removeChild($node);
	return $next;
  }
  
  function mkTbl($node,$snm,$hlp,$link="")
  {
	$doc=$node->ownerDocument;
	$tb=nuNode($node,"table",array("width","height"),array("border"=>"1"),$hlp);
	$tr=$doc->createElement("tr");
	$td=$doc->createElement("td");
	if (!empty($link))
	{
		$a=$doc->createElement("a");
		$lnk=$doc->createAttribute("href");
		$lnk->appendChild($doc->createTextNode($link));
		$a->appendChild($lnk);
		$a->appendChild($doc->createTextNode($snm));
		$td->appendChild($a);
	}else
		$td->appendChild($doc->createTextNode($snm));
	$tr->appendChild($td);
	$tb->appendChild($tr);
	return $tb;
  }
  
  function replaceImg($node)
  {
	$src=$node->getAttribute("src");
	$alt=$node->getAttribute("alt");
	$lnk="";
	$link="";
    $par=$node->parentNode;
	if ($par->nodeName=="a")
	{
		$lnk="\n".$par->getAttribute("title");
		$xl=explode(" ",$par->getAttribute("title"));
		$link=$xl[1];
	}
	return mkTbl($node,"img:".$alt,"image ".$src.$lnk,$link);
  }
  function replaceObj($node)
  {
	$cls=$node->getAttribute("classid");
	$xnd=$node->firstChild;
	$pars="";
	while($xnd)
	{
		if ($xnd->nodeName=="param")
		$pars.=$xnd->getAttribute("name")."=".$xnd->getAttribute("value");
		$xnd=$xnd->nextSibling;
	}
	return mkTbl($node,"obj:".$cls,"obj:".$cls.":".$pars);
  }
  function replaceEmbed($obj)
  {
	$src=$node->getAttribute("src");
	$tp=$node->getAttribute("type");
	return mkTbl($node,"emb:".$src,"embed:".$tp.":".$src);
  }

  function ProcessNode($nd)
  {
    global $mode,$cururl,$curhost;
	$nm=strtolower($nd->nodeName);
	$leave=true;
	$next=$nd->nextSibling;
	if ($nm=="script" || ($nm=="noindex" && $mode==0) || $nm=="style" || $nm=="iframe" || $nm=="param")
	  $leave=false;
	if ($leave)
	{
		if ($nm=="img")
			$nd=replaceImg($nd);
		if ($nm=="object")
			$nd=replaceObj($nd);
		if ($nm=="embed")
			$nd=replaceEmb($nd);
		if ($nm=="table" || $nm=="td" || $nm=="tr" || $nm=="th" || $nm=="div")
			$nd=nuNode($nd,$nm,array("width","height","align","valign","colspan","rowspan"));
		if ($nm=="a")
		{
			$href=$nd->getAttribute("href");
			$mhref=$href;
			if (strpos($mhref,"javascript:")===false && ($nd->getAttribute("rel")!="nofollow" || $mode!=1))
			{
				if (strpos("://",$mhref)===false){;}
				else{
				  if ($mhref[0]=='/')
				    $mhref=sprintf("http://%s%s",$curhost,$mhref);
				  else{
				    $pars=explode("?",$cururl);
					$st=$pars[0];
					if ($st[strlen($st)-1]=='/')
						$st=substr($st,0,strlen($st)-1);
					else{
					  $pars=explode("/",$st);
					  $st=implode("/",array_slice($pars,0,-1));
					}
					$mhref=sprintf("%s/%s",$st,$mhref);
				  }
				}
				$nd=nuNode($nd,"a",null,array("href"=>sprintf("%s?method=get&url=%s&viewAs=%d&referer=%s",$_SERVER['PHP_SELF'],$mhref,$mode,$cururl)),"link ".$href);
			}else{
				$next=killNode($nd);
				$nd=null;
			}
		}
		if ($nm=="font" || $nm=="noscript")
		{
			$next=killNode($nd);
			$nd=null;
		}
		if ($nd)
		{
		$cnd=$nd->firstChild;
		while($cnd)
		{
		  $cnd=ProcessNode($cnd);
		}
		}
		return $next;
	}
	$nd->parentNode->removeChild($nd);
	return $next;
  }
  
 if ($doc && $xbody)
 {
	ProcessNode($xbody);
// $mdoc=new DOMDocument("1.0","utf-8");
// $mdoc->encoding="utf-8";
// $mdoc->appendChild($mdoc->importNode($xbody,true));
 $str=$doc->saveHTML();
 unset($doc);
 //unset($mdoc);
 $dt=explode("<body>",$str);
 $dt=explode("</body>",$dt[1]);
 printf("%s",$dt[0]);
 }
?>

</td></tr></table>
</body>
</html>

